import Vue from 'vue'
import { ipcRenderer } from 'electron'
import { createPinia, PiniaVuePlugin } from 'pinia'
import Router from 'vue-router'

import App from './App'

import store from './store/index.js'
// 引用element
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 日志
import './error'
import './icons'
import '@/styles/index.scss'
import '@/styles/dark-mode.scss'


// 引入 i18n 语言包
import VueI18n from 'vue-i18n'
import loadLanguage from "./i18n"

const languages = loadLanguage()


import router from './router/index'

const pinia = createPinia()

if (!process.env.IS_WEB) {
  ipcRenderer.invoke("IsUseSysTitle").then(res => {
    if (!res) {
      require('@/styles/custom-title.scss')
    }
  });
}
Vue.use(PiniaVuePlugin) // 确保pinia在最先挂载
// 创建 i18n
Vue.use(VueI18n) // 新版本必须要这个，不知道为什么

Vue.use(Router)

if (!localStorage.language){ localStorage.language = 'zh-CN'}


if (!localStorage.printer){
  ipcRenderer.invoke("getPrinterList").then(list => {
    for (let i = 0; i < list.length; i++) {
      if (list[i].isDefault){
        localStorage.printer = list[i].name;
      }
    }
    if (!localStorage.printer && list.length>0){
      localStorage.printer = list[0].name;
    }
  })
}


const i18n = new VueI18n({
  locale: localStorage.language, // 设置默认语言
  messages: languages, // 设置语言包
});
Vue.use(ElementUI, {
  i18n: (key, value) => i18n.t(key, value)
})

Vue.config.productionTip = false




Vue.prototype.a = function(url){
  this.$router.push({ path: url });
}

Vue.prototype.rand = function(start, end) {
  // 确保 start 和 end 是整数
  start = Math.ceil(start);
  end = Math.floor(end);

  // 生成范围在 [start, end] 之间的随机整数
  return Math.floor(Math.random() * (end - start + 1)) + start;
}


Vue.prototype.toast = function(content){
  const h = this.$createElement;
  this.$notify({
    title: this.$t('提示'),
    message: h('i', { style: 'color: teal'}, content)
  });

}

Vue.prototype.err = function(content){
  this.$message.error(content);
}

window.addEventListener('keydown', function (event) {


  if (event.key === 'Escape') {
    router.back();
  }
});


new Vue({
  components: { App },
  store,
  router,
  pinia,
  i18n,
  template: '<App/>',
}).$mount('#app')

